﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Infrastructure;
using WsdShop.IService;
using WsdShop.Model;
using WsdShop.Model.SearchModels;

namespace WsdShop.Service
{
    public class WxImageTextService : IWxImageTextService
    {
        public List<WxImageText> GetAllImageText()
        {
            return BaseService.LoadEntities<WxImageText>(m => m.IsMain);
        }

        public List<WxImageText> ImageTextManage(ImageTextSearchModel search, out int total)
        {
            StringBuilder wherestr = new StringBuilder();
            wherestr.Append(" IsMain=1 ");
            if (!string.IsNullOrWhiteSpace(search.Keywords))
            {
                wherestr.AppendFormat(" and Title like '%{0}%' or Description like '%{0}%' ", search.Keywords);
            }
            //int totalcount = 0;
            return BaseService.LoadPageEntities<WxImageText>(search.Skip, search.Take, out total, wherestr.ToString(), search.OrderByFields);
        }

        public List<WxImageText> GetByGroupid(string GroupId)
        {
            return BaseService.LoadEntities<WxImageText>(m => m.GroupId == GroupId);
        }

        public Result DeleteByGroupid(string GroupId)
        {
            var list = BaseService.LoadEntities<WxImageText>(m => m.GroupId == GroupId);
            if (list != null && list.Count > 0)
            {
                foreach (var item in list)
                {
                    item.IsDeleted = true;
                    BaseService.UpdateEntity(item);
                }
                return Result.Success();
            }
            return Result.Failure("更新失败");
        }

        public Result DeleteById(int Id, string GroupId)
        {
            var model = BaseService.LoadEntity<WxImageText>(m => m.Id == Id && m.GroupId == GroupId);
            if (model != null)
            {
                model.IsDeleted = true;
                if (BaseService.UpdateEntity(model))
                {
                    return Result.Success();
                }
            }
            return Result.Failure("更新失败");
        }

        public Result SaveAllImageText(IList<WxImageText> ImageTexts)
        {
            if (ImageTexts.Count > 8)
            {
                return Result.Failure("文章数量不能超过8个!");
            }

            if (ImageTexts.All(e => !e.IsMain))
            {
                ImageTexts.First().IsMain = true;
                ImageTexts.First().GroupId = Guid.NewGuid().ToString();
            }
            else
            {
                ImageTexts.First().GroupId = ImageTexts.Where(m => m.IsMain).First().GroupId;
            }
            foreach (var requestImageText in ImageTexts)
            {
                requestImageText.GroupId = ImageTexts.First().GroupId;
                if (requestImageText.Id == 0)
                {
                    BaseService.AddEntity(requestImageText);
                }
                else
                {
                    BaseService.UpdateEntity(requestImageText);
                }
            }
            return Result.Success();
        }
    }
}
